#include <iostream>
#define MAXN 100
using namespace std;
int N, M;
char s[MAXN], t[MAXN];
int dp[MAXN][MAXN];
void solve();

int main() {
    cin >> N >> M >> s >> t;
    solve();
    return 0;
}


// dp[i][j] = {dp[i-1}[j-1]+1, dp[i-1][j], dp[i][j-1]}
void solve() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
            if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]);
        }
    }
    cout << dp[N][M] << endl;
}